
using Nop.Core;
using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Core.Domain.XJ;
using Nop.Services.Events;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Services.XJ
{
	/// <summary>
	/// XJPlaceSign service
	/// </summary>
	public partial class XJPlaceSignService : IXJPlaceSignService
	{
		#region Constants

		/// <summary>
		/// Key for caching
		/// </summary>
		private const string XJPLACESIGNS_ALL_KEY = "Nop.xjplacesign.all";
		/// <summary>
		/// Key for caching
		/// </summary>
		/// <remarks>
		/// {0} : xJPlaceSign ID
		/// </remarks>
		private const string XJPLACESIGN_BY_ID_KEY = "Nop.xjplacesign.id-{0}";
		/// <summary>
		/// Key pattern to clear cache
		/// </summary>
		private const string XJPLACESIGNS_PATTERN_KEY = "Nop.xjplacesign.";
		#endregion

		#region Fields
		private readonly IRepository<XJPlaceSign> _xJPlaceSignRepository;
		private readonly IEventPublisher _eventPublisher;
		private readonly ICacheManager _cacheManager;
		#endregion

		#region Ctor

		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="cacheManager">Cache manager</param>
		/// <param name="xJPlaceSignRepository">XJPlaceSign repository</param>
		/// <param name="eventPublisher">Event published</param>
		public XJPlaceSignService(ICacheManager cacheManager,
			IRepository<XJPlaceSign> xJPlaceSignRepository,
			IEventPublisher eventPublisher)
		{
			this._cacheManager = cacheManager;
			this._xJPlaceSignRepository = xJPlaceSignRepository;
			this._eventPublisher = eventPublisher;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Gets all XJPlaceSigns
		/// </summary>
		/// <returns>XJPlaceSigns</returns>
		public virtual IList<XJPlaceSign> GetAllXJPlaceSigns()
		{
			string key = XJPLACESIGNS_ALL_KEY;
			return _cacheManager.Get(key, () =>
			{
				var query = from a in _xJPlaceSignRepository.Table
							orderby a.Id
							select a;
				return query.ToList();
			});
		}

		public virtual IPagedList<XJPlaceSign> GetAllPlaceSigns(int placeId = 0, int pageIndex = 0, int pageSize = int.MaxValue)
		{
			var query = _xJPlaceSignRepository.Table;
			if (placeId > 0)
				query = query.Where(m => m.PlaceId == placeId);
			query = query.OrderByDescending(c => c.Id);

			//paging
			return new PagedList<XJPlaceSign>(query, pageIndex, pageSize);
		}

		/// <summary>
		/// Gets a XJPlaceSign 
		/// </summary>
		/// <param name="xJPlaceSignId">XJPlaceSign identifier</param>
		/// <returns>XJPlaceSign</returns>
		public virtual XJPlaceSign GetXJPlaceSignById(int xJPlaceSignId)
		{
			if (xJPlaceSignId == 0)
				return null;

			string key = string.Format(XJPLACESIGN_BY_ID_KEY, xJPlaceSignId);
			return _cacheManager.Get(key, () => _xJPlaceSignRepository.GetById(xJPlaceSignId));
		}

		/// <summary>
		/// Inserts a XJPlaceSign
		/// </summary>
		/// <param name="xJPlaceSign">XJPlaceSign</param>
		public virtual void InsertXJPlaceSign(XJPlaceSign xJPlaceSign)
		{
			if (xJPlaceSign == null)
				throw new ArgumentNullException("xJPlaceSign");

			_xJPlaceSignRepository.Insert(xJPlaceSign);

			_cacheManager.RemoveByPattern(XJPLACESIGNS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityInserted(xJPlaceSign);
		}

		/// <summary>
		/// Updates the XJPlaceSign
		/// </summary>
		/// <param name="xJPlaceSign">XJPlaceSign</param>
		public virtual void UpdateXJPlaceSign(XJPlaceSign xJPlaceSign)
		{
			if (xJPlaceSign == null)
				throw new ArgumentNullException("xJPlaceSign");

			_xJPlaceSignRepository.Update(xJPlaceSign);

			_cacheManager.RemoveByPattern(XJPLACESIGNS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityUpdated(xJPlaceSign);
		}

		/// <summary>
		/// Deletes a XJPlaceSign
		/// </summary>
		/// <param name="xJPlaceSign">XJPlaceSign</param>
		public virtual void DeleteXJPlaceSign(XJPlaceSign xJPlaceSign)
		{
			if (xJPlaceSign == null)
				throw new ArgumentNullException("xJPlaceSign");

			_xJPlaceSignRepository.Delete(xJPlaceSign);

			_cacheManager.RemoveByPattern(XJPLACESIGNS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityDeleted(xJPlaceSign);
		}

		#endregion
	}
}
